package com.ww.springboot.boot.thread.study.tools;

import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.*;

/**
 * todo
 *
 * @author wanwei
 * @since 2021-04-28 17:24
 */
public class FutureDemo {

    private static final ExecutorService executorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, Runtime.getRuntime().availableProcessors() * 2,
            10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(), new CustomizableThreadFactory("platformStore-thread-pool"));


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("开始" + System.currentTimeMillis());
        List<Future> futures = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Future future = executorService.submit(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {

                }
                return 1;
            });
            //future.get() 会堵塞线程
            //System.out.println(future.get());
            futures.add(future);
        }

        for(Future future:futures){
            System.out.println(future.get());
        }
        executorService.shutdown();
        System.out.println("结束" + System.currentTimeMillis());
    }
}
